/*
 *   Copyright (c) 2014, Andrew Romanenko <melanhit@gmail.com>
 *   All rights reserved.
 *
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright notice, this
 *      list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright notice,
 *      this list of conditions and the following disclaimer in the documentation
 *      and/or other materials provided with the distribution.
 *   3. Neither the name of the project nor the names of its contributors
 *      may be used to endorse or promote products derived from this software
 *      without specific prior written permission.
 *
 *   THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND
 *   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *   DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 *   ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdlib.h>
#include <stdint.h>
#include <string.h>

#include "../akmos.h"
#include "../cipher.h"

#include "ofb.h"

int akmos_ofb_init(akmos_cipher_t *ctx)
{
    akmos_ofb_t *ptr;

    ptr = (akmos_ofb_t *)ctx->mctx;

    ptr->iv = malloc(ctx->algo->blklen);
    if(!ptr->iv)
        return AKMOS_ERR_ENOMEM;

    return AKMOS_ERR_SUCCESS;
}

void akmos_ofb_setiv(akmos_cipher_t *ctx, const uint8_t *iv)
{
    akmos_ofb_t *ptr;

    ptr = (akmos_ofb_t *)ctx->mctx;

    if(!iv)
        memset(ptr->iv, 0, ctx->algo->blklen);
    else
        memcpy(ptr->iv, iv, ctx->algo->blklen);
}

void akmos_ofb_encrypt(akmos_cipher_t *ctx, const uint8_t *in_blk, size_t in_len, uint8_t *out_blk)
{
    akmos_ofb_t *ptr;
    size_t i, j, n, y;
    uint32_t *p1, *p2, *p3;

    ptr = (akmos_ofb_t *)ctx->mctx;

    n = (in_len / ctx->algo->blklen);
    y = ctx->algo->blklen / sizeof(uint32_t);

    p1 = (uint32_t *)in_blk;
    p2 = (uint32_t *)out_blk;

    for(i = 0; i < n; i++) {
        ctx->algo->encrypt(ctx->actx, ptr->iv, ptr->iv);

        p3 = (uint32_t *)ptr->iv;
        for(j = 0; j < y; j++, p1++, p2++)
            *p2 = *p1 ^ p3[j];
    }

    n = in_len - (n * ctx->algo->blklen);
    ctx->algo->encrypt(ctx->actx, ptr->iv, ptr->iv);

    out_blk = (uint8_t *)p2;
    in_blk  = (uint8_t *)p1;
    for(j = 0; j < n; j++, out_blk++, in_blk++)
        *out_blk = *in_blk ^ ptr->iv[j];
}

void akmos_ofb_free(akmos_cipher_t *ctx)
{
    akmos_ofb_t *ptr;

    if(!ctx)
        return;

    ptr = (akmos_ofb_t *)ctx->mctx;

    if(ptr->iv) {
        memset(ptr->iv, 0, ctx->algo->blklen);
        free(ptr->iv);
    }

    memset(ptr, 0, sizeof(akmos_ofb_t));
    free(ptr);
}
